package com.qingke.notlonely.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class NotLonelyBottomDAO {
	protected static SessionFactory sf=null;
	protected static Session session=null;
	
	static {
		sf = new Configuration().configure().buildSessionFactory();
		session = sf.openSession();
	}
	
	public NotLonelyBottomDAO(){
		init();
	}
	
	private void init(){

	}
	
	
	public void close(){
		if(session!=null){
			session.close();
		}
	}
	
	
	public Criteria getCriteria(Class<?> classType){
		DetachedCriteria detachCriteria=DetachedCriteria.forClass(classType);
		Criteria criteria=detachCriteria.getExecutableCriteria(session);
		return criteria;
	}
	

	
	public <T> List<T> get(Class<T> classType,Map<String,Object> map){
		DetachedCriteria detachCriteria=DetachedCriteria.forClass(classType);
		Criteria criteria=detachCriteria.getExecutableCriteria(session);
		if(map!=null)
			criteria.add(Restrictions.allEq(map));
		@SuppressWarnings("unchecked")
		List<T> list=criteria.list(); 
		return list;
	}
	
	public <T> List<T> getAll(Class<T> classType){
		return get(classType,null);
	}
	
	public <T> T getById(Class<T> classType,int id){
		Map<String,Object> map=new HashMap<>();
		map.put("id",id);
		List<T> list=get(classType,map);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	public <T> boolean delete(T t){
		Transaction tx=session.beginTransaction();
		try{
			session.delete(t);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public boolean delete(List<?> list){
		Transaction tx=session.beginTransaction();
		try{
			for(Object o:list){
				session.delete(o);
			}
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public <T> boolean update(T t){
		Transaction tx=session.beginTransaction();
		try{
			session.update(t);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public boolean update(List<?> list){
		Transaction tx=session.beginTransaction();
		try{
			for(Object o:list){
				session.update(o);
			}
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public boolean update(Set<?> list){
		Transaction tx=session.beginTransaction();
		try{
			for(Object o:list){
				session.update(o);
			}
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public <T> boolean save(T t){
		Transaction tx=session.beginTransaction();
		try{
			session.save(t);
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	
	public boolean save(List<?> list){
		Transaction tx=session.beginTransaction();
		try{
			for(Object o:list){
				session.save(o);
			}
			tx.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
			return false;
		}
		return true;
	}
	public void clear(){
		session.clear();
	}
}
